<!-- user表单弹窗 -->
<form id="customer-form" lay-filter="customer-form"
	class="layui-form model-form">
	<input name="id" type="hidden" />
	<input name="customerCode" type="hidden" />
	<input name="createId" type="hidden" />
	<input name="customerStatus" type="hidden" />
	

	
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">姓名</label>
			<div class="layui-input-block">
				<input name="customerName" placeholder="请输入顾客姓名" type="text"
				class="layui-input" lay-verify="required" />
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">性别</label>
			<div class="layui-input-block">
				<input type="radio" name="customerSex" value="1" title="男" />
			    <input type="radio" name="customerSex" value="0" title="女" />
			</div>
		</div>
	</div>
	
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">手机号</label>
			<div class="layui-input-block">
				<input id="customerPhone" name="customerPhone" placeholder="请输入顾客手机号" type="text"
				class="layui-input" lay-verify="required|phone|number" />
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">生日</label>
			<div class="layui-input-block">
				<input type="text" lay-verify="required" name="customerBirthday" id="date1" autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>
	
	<div class="layui-form-item verificationCode">
		<div class="layui-inline">
			<label class="layui-form-label">验证码</label>
			<div class="layui-input-block">
				<input  name="verificationCode" placeholder="请输入验证码" type="text"
				class="layui-input"/>
			</div>
		</div>
		<div class="layui-inline">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" id="smsSend">发送验证码</button>
			</div>
		</div>
	</div>
	
	<div class="layui-form-item">
			<label class="layui-form-label">地址</label>
			<div class="layui-input-block">
				<input name="customerAddress" placeholder="请输入顾客地址" type="text"
					class="layui-input" lay-verify="required" />
			</div>
	</div>
	<div class="layui-form-item model-form-footer">
		<button class="layui-btn layui-btn-primary" type="button"
			ew-event="closeDialog">取消</button>
		<button class="layui-btn" lay-filter="form-submit" lay-submit>保存</button>
	</div>
</form>
<script>
	layui.use([ 'layer', 'admin', 'form', 'formSelects', 'upload' ,'laydate'],
					function() {
						var $ = layui.jquery;
						var layer = layui.layer;
						var admin = layui.admin;
						var form = layui.form;
						var upload = layui.upload;
						var laydate = layui.laydate;
						var formSelects = layui.formSelects;
						var roleId = $("#roleId").attr("roleId");
						

						form.render('radio');
						form.render('select');
						laydate.render({
						    elem: '#date1'
						  });

						var url = 'system/customer/add';
						// 回显数据
						var customer = admin.getTempData('t_customer');
						if (customer) {
							var url = 'system/customer/update';
							if(roleId != 2 && roleId != 5){
								$("input[name=customerPhone]").attr("disabled","disabled").addClass("layui-disabled");
								$("input[name=verificationCode]").attr("lay-verify","required|number");
							}
							if(roleId == 5){
								$("input[name=customerPhone]").attr("disabled","disabled").addClass("layui-disabled");
							}
							$(".verificationCode").remove();
							$("input[name=customerSex][value=0]").attr("checked", customer.customerSex == 0 ? true : false);
			                $("input[name=customerSex][value=1]").attr("checked", customer.customerSex == 1 ? true : false);
							form.val('customer-form', customer);
						}

						// 表单提交事件
						form.on('submit(form-submit)', function(data) {
							layer.load(2);
							data.field.shopId = $("#shopInfo").attr("data");
							admin.req(url, data.field, function(data) {
								layer.closeAll('loading');
								if (data.code == 1) {
									layer.msg(data.msg, {
										icon : 1
									});
									admin.finishPopupCenter();
								} else {
									layer.msg(data.data, {
										icon : 2
									});
								}
							}, "post");
							return false;
						});
						
						$("#smsSend").click(function () {
							var url = 'system/customer/sms';
							var customerPhone = $("#customerPhone").val();
							if(!(/^1[34578]\d{9}$/.test(customerPhone))){ 
								layer.msg("手机号码有误，请重填");  
						        return false; 
						    } 
							
							admin.req(url,{customerPhone: customerPhone}, function(data) {
								layer.closeAll('loading');
								if (data.code == 1) {
									layer.msg('获取验证码成功');
									time();
								} else {
									layer.msg('获取验证码失败');
								}
							}, "post");
							return false;
						})
					});
	
					var wait=60;
				    function time(){
				    	
				        if (wait==0) {
				        	$("#smsSend").attr("disabled",false); 
				        	$("#smsSend").html("获取验证码");
				        	$("#smsSend").css("background","#fe9900");
				            wait=60;
				        }else{
				        	$("#smsSend").attr("disabled", true);
				        	$("#smsSend").html(wait+"秒后重新获取");
				        	$("#smsSend").css("background","#8f8b8b");
				            wait--;
				            setTimeout(function(){
				                this.time();
				            },1000)
				        }
				    }
	

</script>